<template>
  <!-- 2ytq优惠券列表 -->
  <div class="breadcrumb">
    <Breadcrumb :aaa="aaa" :msgg="msg" :txt="txt" :path1="path1" />
  </div>
  <div class="search">
    <!-- 卡片1 -->
    <div class="search_container">
      <el-card>
        <div class="search_top">
          <span> <i></i> <span> 筛选搜索</span> </span>
          <span>
            <el-button @click="onClear">重置</el-button>
            <el-button type="primary" @click="onSearch">查看搜索</el-button>
          </span>
        </div>
        <el-form label-width="140px" class="f">
          <!-- 1 -->
          <el-form-item label="优惠券名称：">
            <el-input type="text" placeholder="优惠券名称" v-model="name"></el-input>
          </el-form-item>
          <!-- 2 -->
          <el-form-item label="优惠券类型：">
            <el-select v-model="type" placeholder="全部">
              <el-option label="全场赠券" value="0" />
              <el-option label="会员赠券" value="1" />
              <el-option label="购物赠券" value="2" />
              <el-option label="注册赠券" value="3" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-card>
    </div>

    <!-- 卡片2 -->
    <el-card class="btn_out">
      <div class="btn">
        <div style="margin: 10px 0">数据列表</div>
        <el-button @click="onCreate" type="primary">添加</el-button>
      </div>
    </el-card>

    <!-- 数据的展示 -->
    <el-table :data="tableData" style="width: 100%" :border="true">
      <!-- 多选框 -->
      <el-table-column type="selection" fixed prop="id" label="" width="60">
      </el-table-column>
      <el-table-column prop="id" label="编号" width="100" />
      <el-table-column prop="name" label="优惠券名称"> </el-table-column>
      <el-table-column prop="type" label="优惠券类型" width="180">
        <template #default="scope">
          <span v-if="scope.row.type === 0">全场赠券</span>
          <span v-if="scope.row.type === 1">会员赠券</span>
          <span v-if="scope.row.type === 2">购物赠券</span>
          <span v-if="scope.row.type === 3">注册赠券</span>
        </template>
      </el-table-column>
      <el-table-column prop="useType" label="可使用商品" width="180">
        <template #default="scope">
          <span v-if="scope.row.useType === 0">全场通用</span>
          <span v-if="scope.row.useType === 1">指定分类</span>
          <span v-if="scope.row.useType === 2">指定商品</span>
        </template>
      </el-table-column>
      <el-table-column prop="" label="使用门槛" width="180">
        <template #default="scope"> 满{{ scope.row.minPoint }}元可用 </template>
      </el-table-column>
      <el-table-column prop="amount" label="面值" width="100">
        <template #default="scope"> {{ scope.row.amount }}元</template>
      </el-table-column>
      <el-table-column prop="platform" label="适用平台" width="100">
        <template #default="scope">
          <span v-if="scope.row.platform === 0 || scope.row.platform == null">全平台</span>
          <span v-if="scope.row.platform === 2">PC平台</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期">
        <template #default="scope">
          <div>
            {{
              scope.row.startTime
              ? dayjs(scope.row.startTime).format("YYYY-MM-DD HH:mm:ss")
              : ""
            }}至
            {{
              scope.row.endTime
              ? dayjs(scope.row.endTime).format("YYYY-MM-DD HH:mm:ss")
              : ""
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column :prop="useStatus" label="状态" width="100">
        <template #default="scope">
          <span v-if="scope.row.endTime > curDate1">未过期</span>
          <span v-else>已过期</span>
        </template></el-table-column>
      <el-table-column fixed="right" label="操作" width="150" flex="right">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="onDetail(scope.row.id)">查看</el-button>
          <el-button link type="primary" size="small" @click="onEdit(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="onDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination class="pagination" v-model:current-page="pagination.pageNum" v-model:page-size="pagination.pageSize"
      background :page-sizes="[5, 10, 15]" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />

    <!-- 查看详情组件 -->
    <Edit :visible="visible" :form="modalForm" @cancel="onCloseDialog" @confirm="onConfirmDialog"></Edit>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { ElTable } from "element-plus";
import type { Ref } from "vue";
import * as api from "../../request/api";
import useTable from "../../hooks/useTables";
import dayjs from "dayjs";
import Edit from "../../components/Edit.vue";
import type { IAdminListItem } from "../../request/QmsAdminConreiller/index";
import type { ICouponListParams } from "../../request/coupon/index";
import router from "../../router";
import { ElMessage, ElMessageBox } from "element-plus";
import { formalDate } from "../../util";
import Breadcrumb from "../../components/breadcrumb.vue";
let msg = ref("优惠券列表");
let txt = ref("营销");
let path1 = ref("sms/flash");
let aaa = ref(0);

let add = false; // false编辑 true 新增
let modalForm = ref({}) as Ref<IAdminListItem>;

//输入框绑定值
let name = ref(""); //优惠券名称
const type = ref(""); //优惠券类型
const useStatus = ref("已过期"); //状态
//查询搜索
let {
  tableData,
  getTableData,
  pagination,
  handleSizeChange,
  handleCurrentChange,
} = useTable(api.coupon.couponList, {
  name,
  type, //useStatus，orderID
});
let onSearch = async () => {
  // console.log(id.value);
  getTableData();
};
//重置
let onClear = () => {
  name.value = "";
  type.value = "";
};
//查看
let onDetail = (id: any) => {
  getTableData();
  router.push({ path: "/sms/couponHistory", query: { id } });
};
//编辑
let onEdit = (id: any) => {
  getTableData();
  router.push({ path: "/sms/updateCoupon", query: { id } });
};
//删除
let onDelete = (row: ICouponListParams) => {
  ElMessageBox.confirm("是否进行删除操作?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      await api.coupon.couponDel(row);
      router.go(0);
      // console.log(detailData)
      // tableData[detailData].splice(detailData, 1) //不用
      ElMessage({
        type: "success",
        message: "删除成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
};
//添加
let onCreate = () => {
  getTableData();
  router.push({ path: "/sms/addCoupon" });
};

// 弹框
let visible = ref(false);

// 添加用户
let onAddUser = () => {
  let newUser = {
    email: "",
    nickName: "",
    note: "",
    password: "",
    status: 0,
    username: "",
  } as IAdminListItem;
  modalForm.value = newUser;
  add = true;
  visible.value = true;
};

// 子级emit组件传过来的事件
let onCloseDialog = () => {
  visible.value = false;
};

// 确认了输入框的内容之后
// 需要发送请求
let onConfirmDialog = (row: IAdminListItem) => {
  // console.log(row);  // row就是表单数据
  // 二次 确认
  ElMessageBox.confirm("是否确认提交!", "提示", {
    confirmButtonText: "OK",
    cancelButtonText: "Cancel",
    type: "warning",
  })
    .then(async () => {
      // 发请求
      // 去判断 是 编辑 还是添加
      if (add) {
        await api.admin.adminRegister(row); // 新增
      } else {
        await api.admin.adminUpdate(row); // 编辑
      }

      ElMessage({
        type: "success",
        message: "操作成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消提交",
      });
    });
};

getTableData();
//是否已过期 获取当前时间
let curDate = new Date();
let curDate1 = formalDate(curDate);
</script>

<style scoped lang="scss">
.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}

.f {
  display: flex;
  justify-content: start;
  flex-wrap: wrap;
}

.bottom {
  width: 100%;
  justify-content: space-between;
}

.delete {
  margin-top: 30px;
}

.el-table--fit {
  border-bottom: 1px solid #ebeef5;
}

.search {
  height: 100vh;
}

:deep .el-table thead {
  font-weight: bold !important;
}

:deep .el-scrollbar__view {
  width: 100% !important;
}

:deep .el-table .el-table__cell {
  text-align: center !important;
}

.el-form--inline .el-form-item {
  margin-right: 10px;
}

.el-form-item__label {
  text-align: right;
  vertical-align: middle;
  float: left;
  font-size: 14px;
  color: #606266;
  line-height: 40px;
  padding: 0 12px 0 0;
  box-sizing: border-box;
}

.el-form-item--mini.el-form-item,
.el-form-item--small.el-form-item {
  margin-bottom: 18px;
}

.el-form--inline .el-form-item__label {
  float: none;
  display: inline-block;
}

.el-form-item__label {
  line-height: 32px;
}

.el-card.is-always-shadow {
  box-shadow: none;
  width: 100%;
}

.search_container {
  left: 0;
  right: 0;
  width: 100%;
}

.breadcrumb {
  height: 45px;
  border-bottom: 1px solid #ebeef5;
  font-size: 20px;
}

.search {
  padding: 20px 15px 20px 15px;
  background-color: #fff;
  margin-bottom: 20px;

  .search_top {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
}

.btn {
  background-color: #fff;
  display: flex;
  justify-content: space-between;
}

.btn_out {
  margin: 20px 0;
}

el-card {
  :deep .el-input {
    width: 200px !important;
  }
}

:deep .el-button {
  font-size: 12px !important;
}
</style>
